AC_INIT(configure.ac)
AM_CONFIG_HEADER(config.h)

VERSION=1.0.0

AM_INIT_AUTOMAKE(historycomponent, $VERSION)

AC_PROG_CXX
AC_PROG_CXXCPP
AC_PROG_CC
AM_PROG_CC_STDC
AC_PROG_INSTALL
AC_DISABLE_STATIC
AM_DISABLE_STATIC
AC_PROG_RANLIB
AC_HEADER_STDC
AM_PROG_LIBTOOL
AC_PROG_CPP
PKG_PROG_PKG_CONFIG

NSPR_VERSION=4.9.6
PIXMAN_VERSION=0.19.2

if test "$PYTHON" == ""; then
    PYTHON=python
fi
AC_SUBST(PYTHON)
echo "Using PYTHON=$PYTHON"

AC_ARG_WITH(engine-path,
    AC_HELP_STRING([--with-engine-path[=path_to_obj_build_dir]],
                   [Specify engine path (default=)]
    ),
    with_engine=$withval, with_engine=
)

OBJ_PATH=
AC_ARG_WITH(engine-path,
[  --with-engine-path[[=DIR]]       use engine obj build in DIR],[
  if test "$withval" != "no" -a "$withval" != "yes"; then
    OBJ_PATH=$withval
  fi
])

NSPR=mozilla-nspr
AC_ARG_WITH([system-nspr],
  [AS_HELP_STRING([--with-system-nspr],
    [Use system installed NSPR(default=no)])
  ]
)

AC_ARG_WITH([system_pixman],
  [AS_HELP_STRING([--with-system_pixman],
    [Use system installed pixman(default=no)])
  ]
)

AS_IF([test "x$with_system_nspr" = "xyes"],
  [PKG_CHECK_MODULES([nspr], [nspr >= $NSPR_VERSION], [NSPR=nspr],
    [AC_MSG_ERROR([--with-system-nspr requested but no nspr(>=$NSPR_VERSION) package found])
  ])
])


AS_IF([test "x$with_system_pixman" = "xyes"],
  [PKG_CHECK_MODULES([pixman], [pixman-1 >= $PIXMAN_VERSION], [PIXMAN=pixman],
    [AC_MSG_ERROR([--with-system-pixman requested but no pixman(>=$PIXMAN_VERSION) package found])
  ])
])

if test "$OBJ_PATH" != ""; then
SDK_DIR=$OBJ_PATH/dist
IDL_DIR=$OBJ_PATH/dist/idl
ENGINE_CFLAGS="-I$OBJ_PATH/dist/include -I$OBJ_PATH/dist/include/mozilla -I$OBJ_PATH/dist/include/dom"
AS_IF([test "x$with_system_nspr" = "xyes"],
  [ENGINE_CFLAGS="${ENGINE_CFLAGS} `pkg-config --cflags-only-I nspr`"],
  [ENGINE_CFLAGS="${ENGINE_CFLAGS} -I$OBJ_PATH/dist/include/nspr"]
)

AS_IF([test "x$with_system_pixman" = "xyes"],
  [ENGINE_CFLAGS="${ENGINE_CFLAGS} `pkg-config --cflags-only-I pixman-1`"],
  [ENGINE_CFLAGS="${ENGINE_CFLAGS} -I$OBJ_PATH/dist/include/pixman"]
)
ENGINE_LIBS="-L$OBJ_PATH/dist/sdk/lib -lxpcomglue_s -lxul"
else
PKG_CHECK_MODULES(ENGINE, libxul $NSPR,
[
    SDK_DIR=`pkg-config --variable=sdkdir libxul`
    IDL_DIR=`pkg-config --variable=idldir libxul`
    INCLUDE_DIR=`pkg-config --variable=includedir libxul`
    ENGINE_CFLAGS="${ENGINE_CFLAGS} -I$INCLUDE_DIR/mozilla/dom"

    AS_IF([test "x$with_system_nspr" = "xyes"],
      [ENGINE_CFLAGS="${ENGINE_CFLAGS} `pkg-config --cflags-only-I nspr`"],
      [ENGINE_CFLAGS="${ENGINE_CFLAGS} -I$INCLUDE_DIR/nspr"]
    )

    AS_IF([test "x$with_system_pixman" = "xyes"],
      [ENGINE_CFLAGS="${ENGINE_CFLAGS} `pkg-config --cflags-only-I pixman-1`"],
      [ENGINE_CFLAGS="${ENGINE_CFLAGS} -I$INCLUDE_DIR/pixman"]
    )
])
fi
AC_SUBST(SDK_DIR)
AC_SUBST(IDL_DIR)


# Required flags
CXXFLAGS="$CXXFLAGS -include mozilla-config.h -std=gnu++0x -fno-rtti -fvisibility=hidden"
# random warnings hide
CXXFLAGS="$CXXFLAGS -Wno-attributes -Wno-ignored-qualifiers -Wall "
CXXFLAGS="$CXXFLAGS -Wno-unused-parameter -Wpointer-arith -Woverloaded-virtual "
CXXFLAGS="$CXXFLAGS -Werror=return-type -Wtype-limits -Wempty-body -Wno-ctor-dtor-privacy "
CXXFLAGS="$CXXFLAGS -Wno-format -Wno-overlength-strings -Wno-invalid-offsetof "
CXXFLAGS="$CXXFLAGS -Wno-variadic-macros -Wno-long-long  -Wno-psabi "
CXXFLAGS="$CXXFLAGS -fno-exceptions "

AC_SUBST(ENGINE_CFLAGS)
AC_SUBST(ENGINE_LIBS)

AC_OUTPUT([
    Makefile
    history/Makefile
    chromehelper/Makefile
    prompt/Makefile
    jscomps/Makefile
    touchhelper/Makefile
    jsscripts/Makefile
    widgetfactory/Makefile
    overrides/Makefile
])
